package nc.ui.levmpoundmanager.poundbillmanager.action;

import java.awt.event.ActionEvent;
import nc.bs.framework.common.NCLocator;
import nc.bs.logging.Logger;
import nc.itf.levmmeampoundbill.poundbill.ILevmPoundBillService;
import nc.ui.levmmeampoundbill.poundbill.model.PoundSettingBillManageModel;
import nc.ui.levmmeampoundbill.poundbill.view.PoundBillForm;
import nc.ui.levmpub.card.WriteCardForAction;
import nc.ui.mmgp.uif2.model.MMGPBillManageModel;
import nc.ui.pub.beans.MessageDialog;
import nc.ui.uif2.NCAction;
import nc.ui.uif2.ShowStatusBarMsgUtil;
import nc.ui.uif2.UIState;
import nc.vo.levmmeampoundbill.poundbill.AggPoundBillVO;
import nc.vo.levmmeampoundbill.poundbill.PoundBillBVO;
import nc.vo.levmmeampoundbill.poundbill.PoundBillVO;
import nc.vo.levmmeampoundbill.poundbill.PushBillStatusEnum;
import nc.vo.ml.NCLangRes4VoTransl;
import nc.vo.mmgp.util.MMStringUtil;
import nc.vo.pub.BusinessException;

public class PoundSettleAction extends NCAction

{
	private PoundBillForm editor;

	public PoundBillForm getEditor() {
		return editor;
	}

	public void setEditor(PoundBillForm editor) {
		this.editor = editor;
	}

	private static final long serialVersionUID = -3051132302789103984L;
	private MMGPBillManageModel model;
	private PoundSettingBillManageModel settingModel;

	public PoundSettleAction() {
		setBtnName(NCLangRes4VoTransl.getNCLangRes().getStrByID("4e10pdm_0", "04e10pdm-0072"));
		setCode("poundBalanceAction");
	}

	public void doAction(ActionEvent e) throws Exception {
		AggPoundBillVO aggVO =null;
		if(this.getEditor().getBillCardPanel().isShowing()) {
			aggVO=(AggPoundBillVO) this.getEditor().getBillCardPanel().getBillValueVO(AggPoundBillVO.class.getName(), PoundBillVO.class.getName(), PoundBillBVO.class.getName());
		}else {
			if (null == this.model.getSelectedOperaDatas() || this.model.getSelectedOperaDatas().length > 1) {
				throw new BusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("4e10pdm_0", "04e10pdm-0039"));
			}
			 aggVO = (AggPoundBillVO) this.model.getSelectedData();
		}


	
		if (!isPushBill(aggVO)) {
			String msg = aggVO.getParentVO().getVpushbillinfo();
			if (MMStringUtil.isNotEmpty(msg)) {
				MessageDialog.showHintDlg(getModel().getContext().getEntranceUI(),
						NCLangRes4VoTransl.getNCLangRes().getStrByID("4e10pdm_0", "04e10pdm-0073"), msg);
			}
		}

		doPoundSettle(aggVO);

		cleanCardInfo();

		showHintMessage();
	}

	private boolean isPushBill(AggPoundBillVO aggVO) {
		PoundBillBVO[] bodyVOs = aggVO.getChildrenVO();
		for (PoundBillBVO bodyVO : bodyVOs) {
			if (bodyVO.getNpushbillstatus() != null
					&& bodyVO.getNpushbillstatus().intValue() == PushBillStatusEnum.PUSH_BILL_YET.toIntValue()) {
				return false;
			}
		}
		return true;
	}

	private void doPoundSettle(AggPoundBillVO aggVO) throws BusinessException {
		ILevmPoundBillService service = (ILevmPoundBillService) NCLocator.getInstance()
				.lookup(ILevmPoundBillService.class);

		AggPoundBillVO retAggVo = service.poundSettle(aggVO);

		getModel().directlyUpdate(retAggVo);
	}

	private void cleanCardInfo() {
		WriteCardForAction writeCardForAction = new WriteCardForAction();
		try {
			writeCardForAction.cleanCardInfo();
		} catch (BusinessException e) {

			Logger.error(e);
		}
	}

	private void showHintMessage() {
		MessageDialog.showHintDlg(getModel().getContext().getEntranceUI(),
				NCLangRes4VoTransl.getNCLangRes().getStrByID("4e10pdm_0", "04e10pdm-0040"),
				NCLangRes4VoTransl.getNCLangRes().getStrByID("4e10pdm_0", "04e10pdm-0074"));
		ShowStatusBarMsgUtil.showStatusBarMsg(
				NCLangRes4VoTransl.getNCLangRes().getStrByID("4e10pdm_0", "04e10pdm-0075"), getModel().getContext());
	}

	public MMGPBillManageModel getModel() {
		return this.model;
	}

	public void setModel(MMGPBillManageModel model) {
		this.model = model;
		this.model.addAppEventListener(this);
	}

	public PoundSettingBillManageModel getSettingModel() {
		return this.settingModel;
	}

	public void setSettingModel(PoundSettingBillManageModel settingModel) {
		this.settingModel = settingModel;
	}

	protected boolean isActionEnable() {
		return (getModel().getUiState() == UIState.NOT_EDIT && getModel().getSelectedData() != null);
	}
}
